package com.fattyCorps.game.service;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.fattyCorps.game.an.AEventListener;
import com.fattyCorps.game.an.AGlobal;
import com.fattyCorps.game.cache.PlayerCache;
import com.fattyCorps.game.exception.game.rank.RankTypeErrorException;
import com.fattyCorps.game.mapper.RankMapper;
import com.fattyCorps.game.mgr.event.EEvent;
import com.fattyCorps.game.obj.cache.PlayerInfo;
import com.fattyCorps.game.obj.db.Ranks;
import com.fattyCorps.game.service.rank.ERankType;
import com.fattyCorps.game.service.rank.Rank;
import com.fattyCorps.game.service.rank.RankElem;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class RankService {

    @AGlobal("gqRank")
    private Rank gqRank;

    @AGlobal("lvRank")
    private Rank lvRank;

    @Autowired
    PlayerCache playerCache;

    @Autowired
    RankMapper rankMapper;

    public List<RankElem> getRankList(Integer type) {
        ERankType rankType = ERankType.values()[type];
        return getRank(rankType).getElems();
    }

    @AEventListener(EEvent.EVENT_UPDATE_GUANQIA)
    public void onGuanQiaInsert(Integer playerId, Integer score) {
        insert(playerId, score, ERankType.GuanQia);
    }

    @AEventListener(EEvent.EVENT_UPDATE_LV)
    public void onLvInsert(Integer playerId, Integer lv) {
        insert(playerId, lv, ERankType.Lv);
    }

    public void insert(Integer playerId, Integer score, ERankType type) {
        Rank rank = getRank(type);
        PlayerInfo playerInfo = playerCache.getPlayerInfo(playerId);

        RankElem re = new RankElem();
        re.setPlayerId(playerId);
        re.setLv(playerInfo.getPlayerData().getLv());
        re.setHead(playerInfo.getPlayerData().getHead());
        re.setNickname(playerInfo.getNickname());
        re.setScore(score);

        rank.insert(re);
    }

    private Rank getRank(ERankType type) {
        switch (type) {
            case GuanQia:
                return gqRank;
            case Lv:
                return lvRank;
        }

        throw new RankTypeErrorException();
    }

//    @AEventListener(EEvent.EVENT_ON_SERVER_START)
//    public void onServerStart() {
//        QueryWrapper<Ranks> wrapper = new QueryWrapper<>();
//        List<Ranks> ranksList = rankMapper.selectList(wrapper);
//
//        for (Ranks ranks : ranksList) {
//            ERankType type = ERankType.values()[ranks.getRankType()];
//            List<RankElem> rankElemList = (List<RankElem>)JSONObject.parseArray(ranks.getInfo(), RankElem.class);
//            getRank(type).init(rankElemList);
//        }
//    }

    @AEventListener(EEvent.EVENT_ON_SERVER_READY_TO_CLOSE)
    public void saveDb() {
        ERankType[] values = ERankType.values();
        for (ERankType value : values) {
            List<RankElem> list = getRank(value).getElems();
            String rankInfo = JSONObject.toJSONString(list);

            QueryWrapper<Ranks> wrapper = new QueryWrapper<>();
            wrapper.eq("rank_type", value.ordinal());
            Ranks ranks = rankMapper.selectOne(wrapper);
            if (ranks == null) {
                ranks = new Ranks();
                ranks.setRankType(value.ordinal());
                ranks.setInfo(rankInfo);
                rankMapper.insert(ranks);
            } else {
                ranks.setInfo(rankInfo);
                rankMapper.update(ranks, wrapper);
            }
        }
    }
}
